/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

/**
 * Classe compagnon de plateau, gère les mechanics de la grille hexagonale
 * @see Source : https://gist.github.com/salamander2/4329783
 */
public class Hexagone {

	private static int BORDERS=0;	//default number of pixels for the border.
	private static int s=0;	// length of one side
	private static int t=0;	// short side of 30o triangle outside of each hex
	private static int r=0;	// radius of inscribed circle (centre to middle of each side). r= h/2
	private static int h=0;	// height. Distance between centres of two adjacent hexes. Distance between two opposite sides in a hex.

	/**
	 * Définit la marge autour de la grille
	 * @param b
	 */
	public static void setBorders(int b){
		BORDERS=b;
	}
	
	/***
	 *  Paramètre les dimensions d'un hexagon en fonction de la longueur de ses côtés
	 * @param side longueur d'un côté
	 */
	public static void setSide(int side) {
		s=side;
		t =  (int) (s / 2);			//t = s sin(30) = (int) CalculateH(s);
		r =  (int) (s * 0.8660254037844);	//r = s cos(30) = (int) CalculateR(s); 
		h=2*r;
	}
	
	/**
	 * Paramètre les dimensions d'un hexagon en fonction de sa hauteur
	 * @param height hauteur de l'hexagone
	 */
	public static void setHeight(int height) {
		h = height;			// h = basic dimension: height (distance between two adj centresr aka size)
		r = h/2;			// r = radius of inscribed circle
		s = (int) (h / 1.73205);	// s = (h/2)/cos(30)= (h/2) / (sqrt(3)/2) = h / sqrt(3)
		t = (int) (r / 1.73205);	// t = (h/2) tan30 = (h/2) 1/sqrt(3) = h / (2 sqrt(3)) = r / sqrt(3)
	}

	/**
	 * 
	 * @param x0 abscisse du centre de la casse
	 * @param y0 ordonnée du centre de la casse
	 * @return (polygone)hexagone
	 */
	public static Polygon getHexagon (int x0, int y0) {

		int y = y0 + BORDERS;
		int x = x0 + BORDERS; 
		int[] cx,cy;

		if (s == 0  || h == 0) {
			System.out.println("ERROR: size of hex has not been set");
			return new Polygon();
		}

		cx = new int[] {x+t,x+s+t,x+s+t+t,x+s+t,x+t,x};	//this is for the whole hexagon to be below and to the right of this point
		cy = new int[] {y,y,y+r,y+r+r,y+r+r,y+r};

		return new Polygon(cx,cy,6);
	}
	
	/**
	 * Dessine les contours d'un hexagone
	 * @param i : colonne de l'hexagone
	 * @param j : ligne de l'hexagone
	 * @param g : Graphics du panel sur lequel dessiner
	 */
	public static void drawHex(int i, int j, Graphics g) {
		int x = i * (s+t);
		int y = j * h + (i%2) * h/2;
		Polygon poly = getHexagon(x,y);
		g.drawPolygon(poly);
	}

	/**
	 * Remplit un hexagone
	 * @param i : colonne de l'hexagone
	 * @param j : ligne de l'hexagone
	 * @param g : Graphics du panel sur lequel dessiner
	 * 
	 */
	public static void fillHex(int i, int j, int n, Graphics g) {

		int x = i * (s+t);
		int y = j * h + (i%2) * h/2;
		if (n < 0) {
			g.setColor(Panneau_Plateau.COLOURBACK);
			g.fillPolygon(getHexagon(x,y));
		}
		if (n > 0) {
			g.setColor(Panneau_Plateau.COLOURCELL);
			g.fillPolygon(getHexagon(x,y));
		}
	}
	/**
	 * Convertit un l'adresse d'un point en px en num de case
	 * @param mx abscisse du point en px
	 * @param my ordonnée du point en px
	 * @return retourne l'adresse de la case de la grille hexagonale
	 */
	public static Point pxtoHex(int mx, int my) {
		Point p = new Point(-1,-1);

		mx -= BORDERS;
		my -= BORDERS;

		int x = (int) (mx / (s+t)); //this gives a quick value for x. It works only on odd cols and doesn't handle the triangle sections. It assumes that the hexagon is a rectangle with width s+t (=1.5*s).
		int y = (int) ((my - (x%2)*r)/h); //this gives the row easily. It needs to be offset by h/2 (=r)if it is in an even column

		/******FIX for clicking in the triangle spaces (on the left side only)*******/
		//dx,dy are the number of pixels from the hex boundary. (ie. relative to the hex clicked in)
		int dx = mx - x*(s+t);
		int dy = my - y*h;

		if (my - (x%2)*r < 0) return p; // prevent clicking in the open halfhexes at the top of the screen

		//even columns
		if (x%2==0) {
			if (dy > r) {	//bottom half of hexes
				if (dx * r /t < dy - r) {
					x--;
				}
			}
			if (dy < r) {	//top half of hexes
				if ((t - dx)*r/t > dy ) {
					x--;
					y--;
				}
			}
		} else {  // odd columns
			if (dy > h) {	//bottom half of hexes
				if (dx * r/t < dy - h) {
					x--;
					y++;
				}
			}
			if (dy < h) {	//top half of hexes
				//System.out.println("" + (t- dx)*r/t +  " " + (dy - r));
				if ((t - dx)*r/t > dy - r) {
				}
			}
		}
		p.x=x;
		p.y=y;
		return p;
	}


}
